package org.example.bitmap;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

import java.io.File;
import java.io.IOException;
import java.util.BitSet;

// 需调整JVM参数 -Xmx1g
public class BitMapDemo {

    private static final long INT_UNSIGN_MAX = ((long) Integer.MAX_VALUE) * 2 + 1;

    public static void main(String[] args) {
        //初始化长度为2 ^ 32位的位数组
        BitSet bitmap = new BitSet((int) (1L << 32));
        //读取QQ号，如果该位为0，标记为1；否则数据重复
        try (LineIterator it = FileUtils.lineIterator(new File("C:\\Users\\rivotek\\PycharmProjects\\PythonProject\\qq2.txt"), "UTF-8")) {
            while (it.hasNext()) {
                String line = it.next();
                if (line.compareTo(String.valueOf(INT_UNSIGN_MAX)) > 0) continue;
                int qq = Integer.parseUnsignedInt(line);
                if (!bitmap.get(qq)) {
                    //数据不存在才set 1，存在则去重了
                    bitmap.set(qq);
                }
                //最后，遍历Bitmap位数组，标记为1的位置就是去重后的结果了
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
